<link rel="stylesheet" href="/stylesheets/admin.css">
<style>
    .has-right {
        color: green;
    }
    .none-right {
        color: red;
    }
</style>
<div class="row">
    <div class="col-xs-12">
        <div class="setting-div">
            <div class="setting-header-div">
                <button id="create-btn" class="create-btn btn btn-default">新增文档</button>
                <a style="position: absolute;right: 50px;height: 34px;width: 80px;" href="/ueditor/classify" id="create-btn" class="list-header-btn btn btn-default">文档分类</a>
                <hr>
            </div>
            <div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>标题</th>
                        <th>分类</th>
                        <th>优先级</th>
                        <th>更新人</th>
                        <th>更新时间</th>
                        <th>创建人</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="list-body">
                    </tbody>
                </table>
            </div>
            <div class="setting-footer-div">

            </div>
        </div>
        <div style="height: 40px"></div>
    </div>
</div>
<link rel="stylesheet" href="/stylesheets/jquery-confirm.min-3.2.3.css">
<script src="/javascripts/jquery-confirm.min-3.1.1.js"></script>
<script language="JavaScript">
    $(function () {
        loadDocList();
    })

    $('#create-btn').click(function () {
        window.location.href = './edit';
    });

    function delClick(node) {
        $.confirm({
            title: '确定要删除该文档？',
            content: '删除后将无法恢复',
            animationBounce: 1.5,
            animation: 'rotateXR',
            closeAnimation: 'rotateYR',
            buttons: {
                ok: {
                    text: "确定",
                    btnClass: 'btn-success',
                    keys: ['enter'],
                    action: function(){
                        var tdNode = node.parentNode.parentNode;
                        var id = tdNode.id;
                        delAction(id)
                    }
                },
                cancel: {
                    text: "取消",
                    action:function(){
                    }
                }
            }
        });
    }

    function delAction(id) {
        $.post('/ueditor/del',{id:id},function (data) {
            if (data.code === 0) {
                showSuccess('已删除');
                loadDocList();
            } else {
                showError(data.message);
            }
        });
    }

    function editClick(node) {
        var tdNode = node.parentNode.parentNode;
        var id = tdNode.id;
        window.location.href = './edit?id=' + id;
    }

    function readClick(node) {
        var tdNode = node.parentNode.parentNode;
        var id = tdNode.id;
        window.location.href = './doc?id=' + id;
    }

    function loadDocList() {
        $.post("/ueditor/list",function (data) {
            if (data.code == 0) {
                var parent = document.getElementById('list-body');
                for (var i = parent.children.length - 1; i >= 0; i--) {
                    parent.removeChild(parent.children[i]);
                }
                for (var i = 0 ; i < data.data.length ; i++) {
                    var doc = data.data[i];
                    var docNode = document.createElement('tr');
                    var id = doc._id;

                    docNode.id = id;

                    var nameNode = document.createElement('td');
                    nameNode.innerText = doc.name;

                    let classifyNode = document.createElement('td');
                    classifyNode.innerText = doc.classify.name;

                    var weightNode = document.createElement('td');
                    let w = ['','置顶','极高','高','普通','低','极低'];
                    weightNode.innerText = w[doc.weight];

                    var updateUser = document.createElement('td');
                    updateUser.innerText = doc.updateUser ? doc.updateUser.nickname : '未知';

                    var updateDate = document.createElement('td');
                    updateDate.innerText = doc.updateTime;

                    var createUser = document.createElement('td');
                    createUser.innerText = doc.createUser ? doc.createUser.nickname : '未知';

                    var createDate = document.createElement('td');
                    createDate.innerText = doc.createTime;

                    var  editNode = document.createElement('td');
                    editNode.innerHTML = "<a onclick='editClick(this)'>编辑</a>&nbsp;&nbsp;<a onclick='readClick(this)'>查看</a>&nbsp;&nbsp;<a onclick='delClick(this)'>删除</a>";

                    docNode.appendChild(nameNode);
                    docNode.appendChild(classifyNode);
                    docNode.appendChild(weightNode);
                    docNode.appendChild(updateUser);
                    docNode.appendChild(updateDate);
                    docNode.appendChild(createUser);
                    docNode.appendChild(createDate);
                    docNode.appendChild(editNode);

                    parent.appendChild(docNode);
                }

            } else  {

            }
        })
    }
</script>